/**
 * 给定每个馅饼出现的时刻、纵坐标、掉落速度和价值
 * 求最大能获得的价值
 * 首先做一个换算，计算出馅饼掉到底的时刻，可以计算出Aij，表示i时刻j位置的价值
 * 注意多个馅饼可能在同一时刻同一位置到底
 * 令Dij是从i时刻j位置开始能够获得的最大价值，则
 * Dij = Aij + max(D[i+1][j-2...j+2])
 * 答案是D[0][W/2+1]
 * 据说多个方案时要优先输出字典序小的
 */
#include <bits/stdc++.h>
using namespace std;

using llt = long long;
using pii = pair<int, int>;
using vi = vector<int>;

llt const INF = 0x1F2F3F4F5F6F7F8F;
llt const NINF = -INF;

int W, H;

vector<vector<llt>> A;
vector<vector<llt>> D;
vector<vector<int>> G;

int chkmax(llt & d, llt b){
    if(b == NINF) return 0;
    if(d < b) return d = b, 1;
    return 0;
}

llt dfs(int time, int pos){
    if(D[time][pos] != NINF) return D[time][pos];

    auto & d = D[time][pos];
    d = 0;
    for(int i=max(1, pos-2);i<=min(W,pos+2);++i){
        if(chkmax(d, dfs(time + 1, i))){
            G[time][pos] = i;
        }
    }

    return d += A[time][pos];
}

void work(){
    cin >> W >> H;
    int time, pos, speed, score;

    A.reserve(1200);
    A.emplace_back(vector<llt>(W + 1, 0));
    A[0][W/2 + 1] = 0;

    while(cin >> time >> pos >> speed >> score){
        if(0 == (H - 1) % speed){
            auto t = time + (H - 1) / speed;
            while(A.size() <= t) A.emplace_back(vector<llt>(W + 1, 0));
            A[t][pos] += score;   
        }        
    }

    int n = A.size();
    D.assign(n, vector<llt>(W + 1, NINF));
    G.assign(n, vector<int>(W + 1, -1));
    copy(A[n-1].begin(), A[n-1].end(), D[n-1].begin());

    llt ans = dfs(0, W / 2 + 1);
    cout << ans << "\n";
    
    int cur = W / 2 + 1;
    for(int nxt, i=0;i<n-1;++i){
        nxt = G[i][cur];
        cout << nxt - cur << "\n";
        cur = nxt;
    }
    return;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int nofkase = 1;
    // cin >> nofkase; 
    while(nofkase--) work();
    return 0;
}
